
package physiqueEngine.bigMath;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

import physiqueEngine.formes.PhysBody;
import physiqueEngine.formes.Physic;

public class Geometry
{
    public static Point2D.Double trigonometricToCartesian(double module, float theta)
    {
        double rad = Math.toRadians(theta);
        return new Point2D.Double(Math.cos(rad) * module, Math.sin(rad) * -module);
    }

    public static double calcPyth(double x, double y)
    {
        return Math.sqrt(x * x + y * y);
    }
    
    public static double caclPyth(Line2D.Double line)
    {
        return Geometry.calcPyth(line.x2 - line.x1, line.y2 - line.y1);
    }

    // TODO A valider
    public static Line2D.Double getPerpendicular(Line2D.Double line)
    {
        return new Line2D.Double(line.x2, line.y2, -line.x1, line.y1);
    }
    
    // TODO a valider
    public static Point2D.Double closestPointonLine(Line2D.Double normal, Point2D.Double h)
    {
        double A,B,C1,C2,det,x,y;
        
        A = normal.y2 - normal.y1;
        B = normal.x2 - normal.x1;
        
        C1 = (normal.y2 - normal.y1)*normal.x1 + (normal.x1 - normal.x2)*normal.y1;
        C2 = -B*h.x + A*h.y;
        
        det = A*A - -(B*B);
        
        if(det != 0)
        {
            x = (A*C1 - B*C2)/det;
            y = (A*C2 - -B*C1)/det;
        }
        else
        {
            x = h.x;
            y = h.y;
        }
        
        return new Point2D.Double(x,y);
    }
    
    public static Point2D.Double lineToVector(Line2D.Double line)
    {
        double x, y;
        
        x = line.x2 - line.x1;
        y = line.y2 - line.y1;
        
        return new Point2D.Double(x,y);
    }
    
}
